package com.spi.soconversion.orders.conversion;

import java.util.ArrayList;
import java.util.Iterator;

import com.spi.soconversion.base.definition.Line;

public class LineFilter implements LineFilterCriteria {

    static private org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(LineFilter.class);

	private int returnLineCount = 0;
	private ArrayList<Line> lines = null;

	public LineFilter(ArrayList<Line> lines) {
		this.lines = lines;
	}

	/**
	 * Filter the lines from an order and return the convertible lines
	 * 
	 * @TODO Need to validate the cases
	 * @return
	 */
	public ArrayList<Line> getFilteredLines() {
		ArrayList<Line> result = new ArrayList<Line>();
		Iterator<Line> itr = lines.iterator();
log.debug("[Source] Total Lines = " + lines.size());	
		ConvertedLine newLine = null;
		while (itr.hasNext()) {
			Line line = itr.next();
			if (line instanceof SaleLine) {
				switch (line.getDefinition()) {
				case LineFilterCriteria.OPEN_SALE_BOOKED_UNPAID:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_SALE_BOOKED_UNPAID,
							MCFLineStatus.NOT_PAID);					
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_SALE_BOOKED_PAID:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_SALE_BOOKED_PAID,
							MCFLineStatus.RELEASED);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_SALE_NOT_BOOKED_PAID:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_SALE_BOOKED_PAID,
							MCFLineStatus.RELEASED);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_SALE_BOOKED_PAID_BACKORDERED:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_SALE_BOOKED_PAID_BACKORDERED,
							MCFLineStatus.BACKORDERED);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_SALE_AWAIT_PICKED:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_SALE_AWAIT_PICKED,
							MCFLineStatus.CLOSED);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_RETURN_NO_EXCHANGE:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_RETURN_NO_EXCHANGE,
							MCFLineStatus.CLOSED);
					// ??? ask james for the status here
					returnLineCount++;
					newLine.setPrimeLineNo("" + returnLineCount);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_RETURN_CORRES_SALE_PULLED_RTR_PICKED:
					if (line instanceof ExchangeLine) { // create return order
														// and exchange order
						if (line.getStatus().equals(COFILineStatus.PICKED)) {
							newLine = new ConvertedLine(
									LineFilterCriteria.OPEN_RETURN_CORRES_SALE_PULLED_RTR_PICKED,
									MCFLineStatus.CLOSED);
						} else if (line.getStatus().equals(
								COFILineStatus.READY_TO_RELEASE)) {
							newLine = new ConvertedLine(
									LineFilterCriteria.OPEN_RETURN_CORRES_SALE_PULLED_RTR_PICKED,
									MCFLineStatus.RELEASED);
						}
						returnLineCount++;
						newLine.setPrimeLineNo("" + returnLineCount);
						result.add(newLine);
					}
					break;
				case LineFilterCriteria.OPEN_RETURN_CORRES_SALE_NOT_PULLED:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_RETURN_CORRES_SALE_NOT_PULLED,
							MCFLineStatus.NOT_PAID);
					// create corresponding exchange line here
					returnLineCount++;
					newLine.setPrimeLineNo("" + returnLineCount);
					result.add(newLine);
					break;
				case LineFilterCriteria.OPEN_RETURN_POND_EXCHANGE:
					newLine = new ConvertedLine(
							LineFilterCriteria.OPEN_RETURN_POND_EXCHANGE,
							MCFLineStatus.RELEASED);
					// create corresponding exchange line here
					returnLineCount++;
					newLine.setPrimeLineNo("" + returnLineCount);
					result.add(newLine);
					break;
				default:
					break;
				}
			}
		}
log.debug("[Filtered] Total Lines = " + result.size());
		return result;
	}
}